package com.lingyistudio.blog.service.impl;

import com.lingyistudio.blog.dao.BlogDao;
import com.lingyistudio.blog.entity.BlogFront;
import com.lingyistudio.blog.entity.CustomerEntity;
import com.lingyistudio.blog.service.BlogService;
import com.lingyistudio.common.response.WebResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName: BlogServiceImpl
 * @Description:
 * @Author: cheng
 * @Date: 2019/5/23
 */

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Resource
    BlogDao blogDao;

    //redis接口
    @Autowired
    RedisTemplate redisTemplate;

    @Override //增加文章
    public WebResponse addBlogService(BlogFront blogFront) {
        try{
            log.info(blogFront.getTokenFront());
            log.info(String.valueOf(redisTemplate.opsForValue().get( "la")));
            log.info( String.valueOf(redisTemplate.opsForValue().get("54656546") ) );
            log.info(String.valueOf(redisTemplate.opsForValue().get("54656546")));
            log.info(String.valueOf(redisTemplate.opsForValue().get( blogFront.getTokenFront())));
            blogFront.setCreatedBy( ((CustomerEntity) redisTemplate.opsForValue().get( blogFront.getTokenFront())).getCustomerId() );//从redis查用户id
            int temp = blogDao.addBlogDao(blogFront);
            if ( temp == 1){
                return WebResponse.success("成功");
            }else if( temp == 0 ){
                return WebResponse.bizError("失败");
            }
        }catch ( Exception e ){
            log.error(" addBlogService :     "+String.valueOf(e));
            return WebResponse.bizError("服务器异常");
        }
        return WebResponse.success("成功");
    }

    @Override //查找某个文章
    public WebResponse findOneBlogService(BlogFront blogFront) {
        try{
            return WebResponse.success("成功" , blogDao.findOneBlogDao(blogFront) ) ;
        }catch (Exception e){
            log.error("findOneBlogService  "+String.valueOf(e));
            return WebResponse.bizError("服务器异常");
        }
    }

    @Override //查找所有文章
    public WebResponse findallBlogNeedCreateByService( BlogFront blogFront ) {
        try{
            return WebResponse.success("成功" , blogDao.findallBlogNeedCreateByDao(blogFront) ) ;
        }catch (Exception e){
            log.error("findallBlogNeedCreateByService  "+String.valueOf(e));
            return WebResponse.bizError("服务器异常");
        }
    }

    @Override //查找所有文章
    public WebResponse findallBlog() {
        try{
            return WebResponse.success("成功" , blogDao.findAllBlogDao() ) ;
        }catch (Exception e){
            log.error("findallBlog  "+String.valueOf(e));
            return WebResponse.bizError("服务器异常");
        }
    }

    @Override //修改文章
    public WebResponse updateBlogService(BlogFront blogFront) {
        try{
            blogFront.setCreatedBy( ((CustomerEntity) redisTemplate.opsForValue().get( blogFront.getTokenFront())).getCustomerId() );
            if( blogDao.updateBlogDao(blogFront) == 1 ){
               return  WebResponse.success("修改成功");
            }
            return  WebResponse.bizError("修改失败");
        }catch ( Exception e ){
            log.error(" updateBlogService: "+e);
            return WebResponse.bizError("服务器异常，请联系管理员");
        }
    }

    @Override //是否发表文章
    public WebResponse updateBlogIsUsed(BlogFront blogFront) {
        try{
            blogFront.setCreatedBy( ((CustomerEntity) redisTemplate.opsForValue().get( blogFront.getTokenFront())).getCustomerId() );
            if( blogDao.updateBlogIsUsedDao(blogFront) == 1 ){
                return  WebResponse.success("修改成功");
            }
            return  WebResponse.bizError("修改失败");
        }catch ( Exception e ){
            log.error(" updateBlogIsUsed: "+e);
            return WebResponse.bizError("服务器异常，请联系管理员");
        }
    }

    @Override  //是否删除文章
    public WebResponse updateBlogIsDeteled(BlogFront blogFront) {
        blogFront.setCreatedBy( ((CustomerEntity) redisTemplate.opsForValue().get( blogFront.getTokenFront())).getCustomerId() );
        try{
            if( blogDao.updateBlogIsDeleteDao(blogFront) == 1 ){
                return  WebResponse.success("修改成功");
            }
            return  WebResponse.bizError("修改失败");
        }catch ( Exception e ){
            log.error(" updateBlogIsDeteled: "+e);
            return WebResponse.bizError("服务器异常，请联系管理员");
        }
    }


}
